<template>
  <div id="UserRegisterPage">
    <div class="login-container">
      <div class="login-card">
        <h2 class="title">智合云图库 - 用户注册</h2>
        <div class="desc">企业级智能协同云图库</div>
        <a-form :model="formState" name="basic" autocomplete="off" @finish="handleSubmit">
          <a-form-item name="userAccount" :rules="[{ required: true, message: '请输入账号' }]">
            <a-input v-model:value="formState.userAccount" size="large" placeholder="请输入账号" />
          </a-form-item>

          <a-form-item
            name="userPassword"
            :rules="[
              { required: true, message: '请输入密码' },
              { min: 8, message: '密码长度不能小于八位' },
            ]"
          >
            <a-input-password
              v-model:value="formState.userPassword"
              size="large"
              placeholder="请输入密码"
            />
          </a-form-item>

          <a-form-item
            name="checkPassword"
            :rules="[
              { required: true, message: '请输入确认密码' },
              { min: 8, message: '确认密码长度不能小于八位' },
            ]"
          >
            <a-input-password
              v-model:value="formState.checkPassword"
              size="large"
              placeholder="请输入确认密码"
            />
          </a-form-item>

          <div class="tips">
            已有账号？
            <RouterLink to="/user/login">去登录</RouterLink>
          </div>

          <a-form-item>
            <a-button type="primary" html-type="submit" class="login-btn">注册</a-button>
          </a-form-item>
        </a-form>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive } from 'vue'
import { useRouter } from 'vue-router'
import { userRegisterUsingPost } from '@/api/userController.ts'
import { message } from 'ant-design-vue'

const formState = reactive<API.UserRegisterRequest>({
  userAccount: '',
  userPassword: '',
  checkPassword: '',
})

const router = useRouter()

/**
 * 提交表单
 * @param values
 */
const handleSubmit = async (values: any) => {
  //校验两次注册密码是否一致
  if(values.userPassword !== values.checkPassword){
    message.error('两次输入的密码不一致');
    return;
  }
  const res = await userRegisterUsingPost(values)
  // 注册成功，跳转到登录页面
  if (res.data.code === 0 && res.data.data) {
    message.success('注册成功')
    router.push({
      path: '/user/login',
      replace: true,
    })
  } else {
    message.error('注册失败，' + res.data.message)
  }
}
</script>

<style scoped>
/* 背景样式 */
#UserRegisterPage {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #1e3c72, #2a5298);
}

/* 注册框 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
}

.login-card {
  width: 380px;
  padding: 32px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  text-align: center;
}

/* 标题 */
.title {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 10px;
}

/* 描述 */
.desc {
  font-size: 14px;
  color: #777;
  margin-bottom: 20px;
}

/* 提示信息 */
.tips {
  text-align: right;
  font-size: 13px;
  color: #555;
  margin-bottom: 10px;
}

.tips a {
  color: #1677ff;
  text-decoration: none;
}

/* 注册按钮 */
.login-btn {
  width: 100%;
  height: 45px;
  font-size: 16px;
  border-radius: 8px;
}

/* 输入框样式 */
:deep(.ant-input) {
  height: 40px;
  border-radius: 8px;
}
</style>
